<?php
/*----------------------------------------------
	This file is to declare CMS Login Module
	CMS -> Login
------------------------------------------------*/

class Login_Holder extends CMS_Holder{
	
	//To declare Page TYpe
	public static $Page_Type = PAGE;
	
	/***
	 * Override function to get Page URL
	***/
	public static function getPageURL(){
		//$url = parent::getPageURL();
		return parent::getPageURL()."Login";	
	}
	
	/***
	 * The override function to get Path
	***/
	public static function getPagePath(){
		return parent::getPagePath()."Login";	
	}
	
	/***
	 * Function to init page
	***/
	protected function init(){
		parent::init();	
		
		//To setup Login HTML Template
		$this->HTML["Content"] = parent::getPagePath()."Login/".TEMPLATE_DIR."Login-Page.html";
	}
	
}//End class


/*------------------------------------------------
 Declare CMSLogin PageController
--------------------------------------------------*/
class Login_Controller extends CMS_Controller{
	
	/***
	 * Function to merge page of Photography
	***/
	protected function MergePage(){
		parent::MergePage();
		
		//TODO Photography Merge Page
		//To merge message
		$this->TBS->MergeField("Message", $_SESSION["Message"]);
	}//End Function
	
	/***
	 * Function to login CMS
	***/
	protected function login(){
		//To setup Return Type
		$this->ReturnType = "REDIRECT";
		
		$login_name = $_REQUEST["name"];
		$password = $_REQUEST["password"];
		
		if(empty($login_name)){
			$_SESSION["Message"] = "Please input login name!";
			//To setup Return Value
			$this->ReturnValue = BASE_URL."CMS/Login";
			return;
		}
		
		if(empty($password)){
			$_SESSION["Message"] = "Please input login password!";
			//To setup Return Value
			$this->ReturnValue = BASE_URL."CMS/Login";
			return;
		}
		
		//To create SQL
		$sql = "SELECT id, name FROM f_staff_tb WHERE login_name='".$login_name."' AND password = '".$password."'";
		$result = $this->DBHandler->Query($sql);
		if(!$result)
			throw new Exception("Check Staff Login Status Error. <b>".$sql."</b>");
		
		if($this->DBHandler->RowsNumber($result) != 1){
			$_SESSION["Message"] = "Login Error! Please check your login name and password.";
			//To setup Return Value
			$this->ReturnValue = BASE_URL."CMS/Login";
			return;
		}
		
		$row = $this->DBHandler->FetchArray($result);
		
		//To setup Login Info
		unset($_SESSION["Message"]);
		$_SESSION["CMS_LOGIN"]["name"] = $row["name"];
		$_SESSION["CMS_LOGIN"]["id"] = $row["id"];
		$this->ReturnValue = BASE_URL."CMS";
		
	}//End Function
	
	/***
	 * Function to logoff CMS
	***/
	protected function logoff(){
		//To setup Return Type
		$this->ReturnType = "REDIRECT";
		
		$_SESSION["Message"] = "See you! Have a nice day! :)";
		unset($_SESSION["CMS_LOGIN"]);
		$this->ReturnValue = BASE_URL."CMS/Login";
		
	}//End Function
	
}//End class




?>